/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.aries.ui.widget.demo.slice;

import com.aries.ui.helper.navigation.NavigationBarUtil;
import com.aries.ui.util.StatusBarUtil;
import com.aries.ui.view.title.TitleBarView;
import com.aries.ui.widget.demo.ResourceTable;
import com.aries.ui.widget.demo.adapter.WidgetAdapter;
import com.aries.ui.widget.demo.base.BaseAbilitySlice;
import com.aries.ui.widget.demo.entity.WidgetEntity;
import com.aries.ui.widget.demo.module.alert.AlertAbilitySlice;
import com.aries.ui.widget.demo.module.radius.RadiusAbility;
import com.aries.ui.widget.demo.module.status.StatusViewHelperAbility;
import com.aries.ui.widget.demo.module.title.TitleAbility;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import ohos.agp.colors.RgbColor;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.VectorElement;
import ohos.agp.window.service.WindowManager;

import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends BaseAbilitySlice {
    private List<WidgetEntity> widgetEntity;
    private ListContainer widgetEntityContainer;
    private WidgetAdapter widgetAdapter;
    private Text mText;
    boolean mIsFull = true;
    private DirectionalLayout parse;

    @Override
    protected void setTitleBar() {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(RgbPalette.RED));

        titleBar.setTitleMainText("UIWidget")
                .setTitleSubText(ResourceTable.String_app_title_content)
                .setStatusBackgroundColor(RgbPalette.WHITE.asArgbInt())
                .setLeftTextDrawable(null)
                .addLeftAction(prepareLeftAction(), 108, 108)
                .setTitleSubTextMarquee(true);
    }

    @NotNull
    private TitleBarView.ViewAction prepareLeftAction() {
        VectorElement vector = new VectorElement(getContext(), ResourceTable.Media_ic_github);
        Image image = new Image(getContext());
        image.setBackground(vector);
        return new TitleBarView.ViewAction(image, component -> {
            Intent intents = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withUri(Uri.parse("https://github.com/AriesHoo/UIWidget/blob/master/README.md"))
                    .withAction(IntentConstants.ACTION_SEARCH)
                    .build();
            intents.setOperation(operation);
            startAbility(intents, 0);
        });
    }

    @Override
    protected void initView(Intent intent) {
        widgetEntityContainer = (ListContainer) findComponentById(ResourceTable.Id_news_container);
        initText();
        initData();
        widgetAdapter = new WidgetAdapter(this, widgetEntity);
        widgetAdapter.addHeaderView(parse, 0, DirectionalLayout.VERTICAL);
        widgetEntityContainer.setItemProvider(widgetAdapter);
        widgetAdapter.notifyDataChanged();
        widgetEntityContainer.setItemClickedListener((listContainer, component, position, l) -> {
            WidgetEntity entity = (WidgetEntity) widgetAdapter.getItem(position);
            if(entity == null) return;
            present(entity.abilitySlice, new Intent());
        });
    }

    private void initText() {
        parse = (DirectionalLayout) LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_layout_main_text, null, false);
        mText = (Text) parse.findComponentById(ResourceTable.Id_main_text);
        mText.setClickedListener(component -> {
            StatusBarUtil.setFullScreen(getWindow(), mContentView, !mIsFull);
            mIsFull = !mIsFull;
        });
        String content = "是否全面屏:" + NavigationBarUtil.isFullScreenDevice(getContext())
                + "\n屏幕纵横比:" + NavigationBarUtil.getAspectRatio(getContext())
                + "\n是否开启全面屏手势:根据判断系统是否开启虚拟导航栏(如华为可手动开关该方法不是完全正确的):"
                + NavigationBarUtil.isOpenFullScreenGestures(getContext())
                + "\n是否有导航栏:" + NavigationBarUtil.hasNavBar(mContentView)
                + "\n导航栏高度:" + NavigationBarUtil.getNavigationBarHeight(getContext())
                + "\n是否隐藏状态栏:" + StatusBarUtil.isStatusBar(mContentView)
                + "\n是否隐藏导航栏:" + NavigationBarUtil.isHideNavigationBar(mContentView);
        mText.setText(content);
//            mText.setText(Html.fromHtml(
//                    "是否全面屏:" + NavigationBarUtil.isFullScreenDevice(mContext)
//                            + "<br>屏幕纵横比:" + NavigationBarUtil.getAspectRatio(mContext)
//                            + "<br>是否开启全面屏手势:根据判断系统是否开启虚拟导航栏(如华为可手动开关该方法不是完全正确的):" + NavigationBarUtil.isOpenFullScreenGestures(mContext)
//                            + "<br>是否有导航栏:" + NavigationBarUtil.hasNavBar(mContext)
//                            + "<br>导航栏高度:" + NavigationBarUtil.getNavigationBarHeight(mContext)
//                            + "<br>是否隐藏状态栏:" + StatusBarUtil.isStatusBar(mContext)
//                            + "<br>是否隐藏导航栏:" + NavigationBarUtil.isHideNavigationBar(mContext)));
    }

    @Override
    protected int getLayout() {
        return ResourceTable.Layout_ability_main;
    }

    private void initData() {
        List<WidgetEntity> list = new ArrayList<>();
        list.add(new WidgetEntity("TabLayout", "快速创建Tab控件包括CommonTabLayout、SlidingTabLayout及SegmentTabLayout三种类型Tab", new SimpleTabAbilitySlice()));
        list.add(new WidgetEntity("StatusViewHelper", "支持顶部自定义标题栏沉浸效果帮助类。", new StatusViewHelperAbility()));
        list.add(new WidgetEntity("KeyboardHelper", "解决软键盘遮住底部输入框问题帮助类。", new TitleEditAbtivity()));
        list.add(new WidgetEntity("TitleBarView", "一款支持沉浸状态栏效果设置的ToolBar控件:支持xml设置是否沉浸、主标题及副标题、左边文字及icon、右边文字及icon、下划线;支持添加左边、中间及右边view方便扩展。", new TitleAbility()));
        list.add(new WidgetEntity("RadiusView", "一款扩展原生TextView、EditText、LinearLayout、FrameLayout、RelativeLayout控件库:主要实现xml设置圆角、手指按下、不可操作样式-减少drawable文件创建;同时支持5.0以上水波纹效果设置。", new RadiusAbility()));
        list.add(new WidgetEntity("UIAlertDialog", "一款自定义Alert效果控件:属性命名及调用方式同原生AlertDialog,增加样式背景及文本相关属性自定义自定义。", new AlertAbilitySlice()));
        list.add(new WidgetEntity("UIActionSheetDialog", "一款底部弹框控件:支持List模式(iOS、微信及QQ样式)和Grid模式", new ActionSheetAbilitySlice()));
        list.add(new WidgetEntity("UIProgressDialog", "一款仿微博、微信、MD loading控件:扩展背景及loading 样式设置。", new LoadingAbilitySlice()));
        widgetEntity = list;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
